package My.Nhom2.ServiceSearch;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.ImageView;


/**
 * The Class ServiceSearchAdapter.
 */
public class ServiceSearchAdapter extends BaseAdapter {

	/** The context. */
	private Context context;

	/** The listresult. */
	private List<DataService> listresult;

	/**
	 * Instantiates a new service search adapter.
	 *
	 * @param context1 the _context
	 * @param list the list
	 */
	public ServiceSearchAdapter(final Context context1,
			final List<DataService> list) {
		context = context1;
		listresult = list;

	}

	public ServiceSearchAdapter(OnItemSelectedListener onItemSelectedListener,
			ArrayList<DataService> list) {
		// TODO Auto-generated constructor stub
		context = (Context) onItemSelectedListener;
		listresult = list;
	}

	/* (non-Javadoc)
	 * @see android.widget.Adapter#getCount()
	 */
	public final int getCount() {
		// TODO Auto-generated method stub
		return listresult.size();
	}

	/* (non-Javadoc)
	 * @see android.widget.Adapter#getItem(int)
	 */
	public Object getItem(int pos) {
		// TODO Auto-generated method stub
		return listresult.get(pos);
	}

	/* (non-Javadoc)
	 * @see android.widget.Adapter#getItemId(int)
	 */
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}
	/* (non-Javadoc)
	 * @see android.widget.Adapter#sort(String)
	 */
	public List<DataService> sortAddress(String typesort) {
		// TODO Auto-generated method stub
		for (int i = 0; i < listresult.size(); i++)
			for (int j = i + 1; j < listresult.size() - 1; j++) {
				int com = listresult.get(i).getTenDiaDiem().compareTo(listresult.get(j).getTenDiaDiem());
				if (com > 0) {
					if (typesort.equals("Tăng dần")) {
						// swap
						Swap(listresult,i,j);
					}
				}
				if (com < 0) {
					if (typesort.equals("Giảm dần")) {
						// swap
						Swap(listresult,i,j);
					}
				}
			}
		return this.listresult;
	}
	/* (non-Javadoc)
	 * @see android.widget.Adapter#sortStreet(String)
	 */
	public List<DataService> sortStreet(String typesort) {
		// TODO Auto-generated method stub
		for (int i = 0; i < listresult.size(); i++)
			for (int j = i + 1; j < listresult.size() - 1; j++) {
				int com = listresult.get(i).getTenDuong().compareTo(listresult.get(j).getTenDuong());
				if (com > 0) {
					if (typesort.equals("Tăng dần")) {
						// swap
						Swap(listresult,i,j);
					}
				}
				if (com < 0) {
					if (typesort.equals("Giảm dần")) {
						// swap
						Swap(listresult,i,j);
					}
				}
			}
		return this.listresult;
	}
	/* (non-Javadoc)
	 * @see android.widget.Adapter#sortNumberAddr(String)
	 */
	public List<DataService> sortNumberAddr(String typesort) {
		// TODO Auto-generated method stub
		for (int i = 0; i < listresult.size(); i++)
		{
			if(listresult.get(i).getSoNha()==null)
				listresult.get(i).setSoNha("0");
			for (int j = i + 1; j < listresult.size() - 1; j++) {
				if(listresult.get(j).getSoNha()==null)
					listresult.get(j).setSoNha("0");
				int com = listresult.get(i).getSoNha().compareTo(listresult.get(j).getSoNha());
				if (com > 0) {
					if (typesort.equals("Tăng dần")) {
						// swap
						Swap(listresult,i,j);
					}
				}
				if (com < 0) {
					if (typesort.equals("Giảm dần")) {
						// swap
						Swap(listresult,i,j);
					}
				}
			}
		}
		return this.listresult;
	}
	/* (non-Javadoc)
	 * @see android.widget.Adapter#sortWard(String)
	 */
	public List<DataService> sortWard(String typesort) {
		// TODO Auto-generated method stub
		for (int i = 0; i < listresult.size(); i++)
			for (int j = i + 1; j < listresult.size() - 1; j++) {
				int com = listresult.get(i).getTenPhuong().compareTo(listresult.get(j).getTenPhuong());
				if (com > 0) {
					if (typesort.equals("Tăng dần")) {
						// swap
						Swap(listresult,i,j);
					}
				}
				if (com < 0) {
					if (typesort.equals("Giảm dần")) {
						// swap
						Swap(listresult,i,j);
					}
				}
			}
		return this.listresult;
	}
	
	/* (non-Javadoc)
	 * @see android.widget.Adapter#sortDistrict(String)
	 */
	public List<DataService> sortDistrict(String typesort) {
		// TODO Auto-generated method stub
		for (int i = 0; i < listresult.size(); i++)
			for (int j = i + 1; j < listresult.size(); j++) {
				int com = listresult.get(i).getTenQuanHuyen().compareTo(listresult.get(j).getTenQuanHuyen());
				if (com > 0) {
					if (typesort.equals("Tăng dần")) {
						// swap
						Swap(listresult,i,j);
					}
				}
				if (com < 0) {
					if (typesort.equals("Giảm dần")) {
						// swap
						Swap(listresult,j,i);
					}
				}
			}
		return this.listresult;
	}
	
	
	
	
	public void Swap(List<DataService> arr, int i, int j) {
		DataService temp = arr.get(i);
		DataService temp2 = arr.get(j);
		arr.remove(i);
		arr.add(i, temp2);
		arr.remove(j);
		arr.add(j, temp);
	}

	/* (non-Javadoc)
	 * @see android.widget.Adapter#getView(int, android.view.View, android.view.ViewGroup)
	 */
	public View getView(int arg0, View arg1, ViewGroup arg2) {
		// TODO Auto-generated method stub
		DataService node = listresult.get(arg0);
		if(arg1 == null) {
			LayoutInflater inflater = LayoutInflater.from(context);
			arg1 = inflater.inflate(R.layout.itemsearch, null);
		}
		TextView tvName = (TextView)arg1.findViewById(R.id.tvName);
		tvName.setText(node.getTenDiaDiem());
		TextView tvType = (TextView)arg1.findViewById(R.id.tvType);
		tvType.setText("["+node.getTenDichVu()+"]");
		TextView tvAddress = (TextView)arg1.findViewById(R.id.tvAddress);
		tvAddress.setText(node.getSoNha()+" "+node.getTenDuong()+", P."+node.getTenPhuong()+
				", Q."+node.getTenQuanHuyen()+", TP."+node.getTenTinhThanh());		
		ImageView imgView = (ImageView)arg1.findViewById(R.id.imgAvatar);
		if(node.getTenDichVu().equals("ATM")){
			imgView.setImageResource(R.drawable.atm);
		}
		if(node.getTenDichVu().equals("Bệnh viện")){
			imgView.setImageResource(R.drawable.pic_hos);
		}
		if(node.getTenDichVu().equals("karaoke")){
			imgView.setImageResource(R.drawable.pic_karaoke);
		}
		if(node.getTenDichVu().equals("Quán ăn")){
			imgView.setImageResource(R.drawable.pic_quan_an);
		}
		if(node.getTenDichVu().equals("Trạm xăng")){
			imgView.setImageResource(R.drawable.pic_cayxang);
		}
		if(node.getTenDichVu().equals("Quán café")){
			imgView.setImageResource(R.drawable.pic_coffee);
		}
		if(node.getTenDichVu().equals("Shop thời trang")){
			imgView.setImageResource(R.drawable.pic_fashion_shop);
		}
		if(node.getTenDichVu().equals("Nhà hàng")){
			imgView.setImageResource(R.drawable.pic_res);
		}
		if(node.getTenDichVu().equals("Khách sạn")){
			imgView.setImageResource(R.drawable.hotel);
		}
		if(node.getTenDichVu().equals("Công viên")){
			imgView.setImageResource(R.drawable.pic_park);
		}
		if(node.getTenDichVu().equals("Chợ")){
			imgView.setImageResource(R.drawable.pic_market);
		}
		if(node.getTenDichVu().equals("Rạp chiếu phim")){
			imgView.setImageResource(R.drawable.pic_cinema);
		}
		if(node.getTenDichVu().equals("Bến xe bus")){
			imgView.setImageResource(R.drawable.pic_bus_stop);
		}
		if(node.getTenDichVu().equals("Sân vận động")){
			imgView.setImageResource(R.drawable.pic_football);
		}
		if(node.getTenDichVu().equals("Phòng khám")){
			imgView.setImageResource(R.drawable.pic_hos);
		}
		



		return arg1;
	}

}
